import { createStore } from 'vuex'
const store = createStore({
  state() {
    return {
      books: [
        { name: 'vue', price: 100, count: 1 },
        { name: 'react', price: 200, count: 2 },
        { name: 'js', price: 300, count: 3 },
      ],
      discount: 1, //默认折扣1
    }
  },
  getters: {
    // 在一个getter中调用另一个getter函数
    // getters中的方法不进行缓存，多次调用会被执行多次
    StoreTotalPrice(state, getters) {
      const Price = state.books.reduce((pre, { price, count }) => pre + price * count, 0)
      console.log(Price)
      return Price * getters.currentDiscount
    },
    currentDiscount(state, getters) {
      return state.discount * 0.5
    },
    // 在getter中返回一个函数
    StoreTotalPriceDiscount(state) {
      return discount => {
        const Price = state.books.reduce((pre, { price, count }) => pre + price * count, 0)
        return Price * discount
      }
    },
  },
})
export default store
